package views.swing;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import models.services.*;
import models.entities.*;
import java.util.List;

public class Selector extends JPanel implements ActionListener{
	private JTextField cCadena=new JTextField(15);
	private JButton bBuscar=new JButton("Buscar");
	private JList lResultados=new JList();
	
	public Selector(){
		bBuscar.addActionListener(this);
		
		JPanel pNorte=new JPanel();
		pNorte.setLayout(new BorderLayout());
		pNorte.add(cCadena, BorderLayout.CENTER);
		pNorte.add(bBuscar, BorderLayout.EAST);
		
		this.setLayout(new BorderLayout());
		this.add(new JScrollPane(lResultados), BorderLayout.CENTER);
		this.add(pNorte, BorderLayout.NORTH);
	}
	
	public void actionPerformed(ActionEvent evt){
		List<Cliente> resultados=new ServicioClientes().buscarPorString("nombre", cCadena.getText());
		lResultados.setListData(resultados.toArray());
	}
	
	public Cliente getClienteSeleccionado(){
		return (Cliente)lResultados.getSelectedValue();
	}
	
	public static void main(String[] args){
		JFrame ventana=new JFrame("Seleccionar cliente");
		ventana.setContentPane(new Selector());
		ventana.pack();
		ventana.setVisible(true);
	}
}